package ar.com.maddes.creenciasadventistas;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import ar.com.maddes.creenciasadventistas.database.Creencia;

import com.actionbarsherlock.app.SherlockListFragment;

public class BeliefFragment extends SherlockListFragment implements
		LoaderManager.LoaderCallbacks<Cursor> {
	/**
	 * The fragment argument representing the section number for this fragment.
	 */
	public static final String ARG_BELIEF_NUMBER = "belief_number";
	public static final String ARG_BELIEF_DESCRIPTION = "belief_description";

	private static final int LOADER_BELIEF_DESCRIPTION = 1;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.fragment_creencia, null);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		// Vaya uno a saber porque no se llama getSupportLoaderManager...
		getLoaderManager().initLoader(1, null, this);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int id, Bundle bundle) {

		switch (id) {
		case LOADER_BELIEF_DESCRIPTION:
			return new CursorLoader(getActivity(),
					Creencia.getUri(getArguments().getInt(ARG_BELIEF_NUMBER)),
					null, null, null, null);
		}
		return null;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {

		switch (loader.getId()) {
		case LOADER_BELIEF_DESCRIPTION:
			cursor.moveToFirst();
			((TextView) getView().findViewById(R.id.textView1)).setText(cursor
					.getString(cursor.getColumnIndex(Creencia.DESCRIPTION)));
			((TextView) getView().findViewById(R.id.textView2)).setText(cursor
					.getString(cursor.getColumnIndex(Creencia.VERSES)));
			
			((LinearLayout) getView().findViewById(R.id.linearLayout1))
					.setVisibility(View.GONE);
			
			((ScrollView) getView().findViewById(R.id.scrollView1))
					.setVisibility(View.VISIBLE);
		}
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {

		return;
	}
}
